package org.huoran.sakamichi.main.controller;

import org.huoran.sakamichi.main.service.UserService;
import org.huoran.sakamichi.main.util.ResultBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller
@RequestMapping({ "/user"})
public class UserController {
	@Resource
	private UserService userService;

	/**
	 * 返回：根据用户名获取用户信息
	 * @param userName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = {"/getUserByUserName/{userName}"})
	public ResultBean getUserByUserName(@PathVariable String userName) {
		return this.userService.getUserByUserName(userName);
	}
	
	/**
	 * 跳转：webSocket测试页面
	 * @return
	 */
	@RequestMapping(value = {"/login"})
	public String login(ModelMap model,String userName,String password) {
		boolean loginSuccess = this.userService.userLogin(userName,password);
		if(loginSuccess){
			return "index";
		}
		else{
			return null;
		}
	}

	/**
	 * 返回：测试事务回滚
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = {"/insertTransaction"})
	public ResultBean insertTransaction() {
		try{
			this.userService.insertTransaction();
			return ResultBean.success("test success");
		} catch (Exception e) {
			return ResultBean.failed("test failed");
		}
	}
}